package max.ubitest.pipe.locators
{
	import max.ubitest.pipe.data.CellData;
	import max.ubitest.pipe.data.ComponentData;
	import max.ubitest.pipe.data.ComponentTypes;
	import max.ubitest.pipe.data.Directions;
	import max.ubitest.pipe.events.PipeEvent;
	import max.ubitest.pipe.view.ComponentRenderer;
	
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.mvcs.Actor;
	
	[Bindable]
	public class ComponentsLocator extends Actor
	{
		public function ComponentsLocator()
		{
			super();
			init();
		}
		
		public var components:ArrayCollection;
		
		[Inspectable(arrayType="max.ubitest.pipe.view.ComponentRenderer")]
		public var mappedComponents:ArrayCollection;
		
		/**
		 * assign when click on a component selector, un-assign when click on a cell
		 */		
		public var selectedComponent:ComponentData;
		
		/**
		 * assign when click on a mapped component, assign this parameter can call out the tool bar
		 * set to null to remove the toolbar
		 */		
		public var edittingComponent:ComponentRenderer;
		
		/**
		 * in order to remove exist toolbar, set edittingComponent to null
		 * 
		 */		
		public function reset():void{
			if(edittingComponent){
				if(edittingComponent.data.cellData){
					edittingComponent.data.cellData.componentData = null;
					
					if(edittingComponent.data.type == ComponentTypes.RESERVOIR){
						var cell:CellData = edittingComponent.data.cellData;
						var evt:PipeEvent = new PipeEvent(PipeEvent.CLEAR_CELLS_AROUND, cell);
						dispatch(evt);
					}
				}
				edittingComponent.data.cellData = null;
				edittingComponent = null;
				dispatch(new PipeEvent(PipeEvent.RESET, null));
			}
		}
		
		private function init():void{
			mappedComponents = new ArrayCollection();
			
			var temp:Array = new Array();
			temp.push(new ComponentData(
				{
					direction:Directions.UP,
					type:ComponentTypes.ONE_PORT,
					sides:[1, 0, 0, 0],
					amount:5,
					width:1,
					height:1
				}
			));
			temp.push(new ComponentData(
				{
					direction:Directions.UP,
					type:ComponentTypes.STRAIGHT,
					sides:[1, 0, 1, 0],
					amount:5,
					width:1,
					height:1
				}
			));
			temp.push(new ComponentData(
				{
					direction:Directions.UP,
					type:ComponentTypes.CORNOR,
					sides:[1, 1, 0, 0],
					amount:5,
					width:1,
					height:1
				}
			));
			temp.push(new ComponentData(
				{
					direction:Directions.UP,
					type:ComponentTypes.THREE_PORT,
					sides:[1, 1, 0, 1],
					amount:5,
					width:1,
					height:1
				}
			));
			temp.push(new ComponentData(
				{
					direction:Directions.UP,
					type:ComponentTypes.WATER_PUMP,
					sides:[1, 0, 0, 0],
					amount:1,
					width:1,
					height:1
				}
			));
			temp.push(new ComponentData(
				{
					direction:Directions.UP,
					type:ComponentTypes.RESERVOIR,
					sides:[0, 1, 0, 0, 0, 0, 0, 0],
					amount:1,
					width:3,
					height:3
				}
			));
			components = new ArrayCollection(temp);
		}
	}
}